package com.sky.controller.admin;

import com.sky.dao.DishDTO;
import com.sky.dao.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVo;
import com.sky.vo.DishWithFlavorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "后台菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("分頁查詢菜品")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageDTO dishPageDTO) {
        PageResult pageResult = dishService.page(dishPageDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("根據分類id查詢菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId) {
        String key = "dish_"+categoryId;
        List<Dish> list = (List<Dish>) redisTemplate.opsForValue().get(key);
        if(list != null && list.size() > 0){
            return Result.success(list);

        }
        list= dishService.listByCategoryId(categoryId);
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }

    @ApiOperation("新增菜品")
    @PostMapping()
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.save(dishDTO);
        String pattern = "dish_" + dishDTO.getCategoryId();
        cleanCache(pattern);
        return Result.success();
    }

    @ApiOperation("根據id查詢菜品")
    @GetMapping("/{id}")
    public Result<DishWithFlavorVO> getById(@PathVariable Long id) {
        DishWithFlavorVO dishWithFlavorVO = dishService.getById(id);
        return Result.success(dishWithFlavorVO);
    }

    @ApiOperation("修改菜品")
    @PutMapping()
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        String pattern = "dish_" + dishDTO.getCategoryId();
        cleanCache(pattern);
        return Result.success();
    }

    @ApiOperation("起售或停售菜品")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, @RequestParam Long id) {
        dishService.startOrStop(status, id);
        String pattern = "dish_*";
        cleanCache(pattern);
        return Result.success();
    }

    @ApiOperation("刪除菜品")
    @DeleteMapping()
    public Result delete(@RequestParam List<Long> ids) {
        dishService.delete(ids);
        String pattern = "dish_*";
        cleanCache(pattern);
        return Result.success();
    }

    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
